<?php

namespace App\Http\Model\Admin;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class TouristAction extends Model
{
    //  table name
    protected $table = 'tourists_actions';

    protected $keyType = 'string';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id','data_status', 'imei', 'idfa', 'search_term', 'page_type', 'visit_time', 'house_province', 'city', 'cityarea_id', 'cityarea2_id', 'house_name', 'unit_price', 'house_pricehouse_room', 'house_hall', 'house_toilet', 'borough_name', 'borough_id', 'house_totalarea', 'house_toward', 'house_topfloor', 'house_floor', 'house_fitment', 'building_type', 'house_type', 'property_right', 'house_address', 'user_behavior', 'house_city', 'cityarea_name', 'cityarea2_name', 'source_id','appName'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];


    /**
     * find one user by id
     *
     * @param  id
     * @return  model
     */
    public function findOneById($id)
    {
        $one = self::find($id);
        return $one;
    }

    /**
     * find list by user_id
     *
     * @param  user_id
     * @return  model list
     */
    public function findListById($user_id)
    {
        $one = self::where('user_id',$user_id)->where('is_del','<>',0)->orderBy('created_at','desc')->take(10)->get();
        return $one;
    }

    /**
     * find one by name
     *
     * @param  name
     * @return  model
     */
    public function findOneByName($name = '')
    {
        $one = self::where('name', $name)->firstOrFail();
        return $one;
    }

    /**
     * find list by pageinate default 15
     *
     * @param  per_page
     * @return page info with  model list
     */
    public function findAllByPaginate($per_page = 15, $input = [])
    {
        return self::paginate($per_page);
    }

    /**
     * update one by id
     *
     * @param  id
     * @param  $input object
     * @return  boolean
     */
    public function updateOneById($id, $input)
    {
        return self::where('id', $id)->update($input);
    }

    public function createNew($input)
    {
        return self::create($input);
    }

    /**
     * create batch
     *
     * @param  input object
     * @return  model
     */
    public function createBatch($input){
        return DB::table(self::getTable())->insert($input);
    }


    // 通过数据状态获取所有数据
    public function getAllByDataStatus($status)
    {
        return self::where('data_status', $status)->get();
    }


    // 通过id设置数据
    public function updateById($phone, $id, $data)
    {
        $this->table = 'tourists_actions_'.($phone % 100);

        return self::where('id', $id)->update($input);
    }



}
